package com.yang.Offer;
import com.yang.Offer.SolutionOffer06.ListNode;
public class Offer25 {
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Offer25Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        // 新建哨兵节点 通过哨兵节点的next进行操作
        ListNode pre = new ListNode(-1), cur = pre;
        while(l1 != null && l2 != null){
            if(l1.val > l2.val){
                cur.next = l2;
                l2 = l2.next;
            }else{
                cur.next = l1;
                l1 = l1.next;
            }
            cur = cur.next;
        }
        // 循环结束 必然存在某一链表没有取出所有节点,因为链表是有序的，所以直接让链表的next接上就行
        cur.next = (l1 == null) ? l2 : l1;
        return pre.next;
    }
}
